package com.example.zbh.dsliao.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.application.App;
import com.example.zbh.dsliao.db.entity.ApplyFriend;

import java.util.List;

/**
 * Created by zbh on 15/11/25.
 */
public class NewFriendMsgAdapter extends RecyclerView.Adapter<NewFriendMsgAdapter.NewFriendViewHolder> {
    private List<ApplyFriend> list;

    public void addData(List<ApplyFriend> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public NewFriendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.dl_recyclerview_apply_notice, null);
        return new NewFriendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(NewFriendViewHolder holder, int position) {
        if (list != null && list.size() > 0){
            ApplyFriend applyFriend = list.get(position);
            holder.name.setText(applyFriend.getName());
            int index = applyFriend.getIndex();
            if (index == 0){
            holder.indicator.setText("同意");
            }else {
                holder.indicator.setText("已添加");
                holder.indicator.setClickable(false);
            }
        }
    }

    public boolean isLength() {
        return list != null && list.size() > 0;
    }

    @Override
    public int getItemCount() {
        return isLength() ? list.size() : 0;
    }

    class NewFriendViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView name;
        private Button indicator;
        public NewFriendViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            indicator = (Button) itemView.findViewById(R.id.indicator);
            indicator.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.indicator:
                    if (listenter !=null){
                        ApplyFriend applyFriend = list.get(getLayoutPosition());
                        listenter.onOnRecyclerItemClick(applyFriend.getName());
                    }
                    break;
            }
        }
    }
    private  OnRecyclerItemClickListenter listenter;
    public void setOnRecyclerItemClickListenter(OnRecyclerItemClickListenter listenter){
        this.listenter = listenter;
    }

    public interface OnRecyclerItemClickListenter{
        void onOnRecyclerItemClick(String name);
    }


}
